package com.realestate.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.mysema.query.BooleanBuilder;
import com.mysema.query.jpa.JPQLQuery;
import com.mysema.query.jpa.impl.JPAQuery;
import com.realestate.constants.ComUnitName;
import com.realestate.constants.RecordStatus;
import com.realestate.model.BrtbProductCommonAttrValue;
import com.realestate.model.QBrtbCommonAttribute;
import com.realestate.model.QBrtbProduct;
import com.realestate.model.QBrtbProductCommonAttrValue;
import com.trg.dao.jpa.GenericDAOImpl;

@Repository
public class ProductCommonAttributeValueDaoImpl extends GenericDAOImpl<BrtbProductCommonAttrValue, Long> implements ProductCommonAttributeValueDao{
	
	@SuppressWarnings("unused")
	private static final Logger LOGGER = Logger.getLogger(ProductCommonAttributeValueDaoImpl.class);
	
	@Override
	@PersistenceContext(unitName = ComUnitName.COM_UNIT)
	public void setEntityManager(EntityManager entityManager) {
		super.setEntityManager(entityManager);
	}

	@Override
	public List<Object[]> getAllCommonAttributesOfAProduct(Long productId) {
		JPQLQuery query = new JPAQuery(em());
		
		QBrtbProduct product = QBrtbProduct.brtbProduct;
		QBrtbCommonAttribute commonAttribute = QBrtbCommonAttribute.brtbCommonAttribute;
		QBrtbProductCommonAttrValue commonAttributeValue = QBrtbProductCommonAttrValue.brtbProductCommonAttrValue;
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(product.recordStatus.eq(RecordStatus.O))
			 .and(commonAttribute.recordStatus.eq(RecordStatus.O))
			 .and(product.id.eq(productId));
		
		query.from(commonAttributeValue)
			.leftJoin(commonAttributeValue.brtbProduct, product)
			.leftJoin(commonAttributeValue.brtbCommonAttribute, commonAttribute)
			.where(where);
		
		List<Object[]> products = query.list(product.id, product.productCode, product.productName, commonAttribute.commonAttributeCode, commonAttribute.commonAttributeName, commonAttributeValue.value);
		return products;
	}

	@Override
	public List<BrtbProductCommonAttrValue> deleteAllCommonAttributeValueOfAProduct(
			Long productId) {
		JPQLQuery query = new JPAQuery(em());
		
		QBrtbProduct product = QBrtbProduct.brtbProduct;
		QBrtbProductCommonAttrValue commonAttributeValue = QBrtbProductCommonAttrValue.brtbProductCommonAttrValue;
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(product.recordStatus.eq(RecordStatus.O))
			 .and(product.id.eq(productId));
		
		query.from(commonAttributeValue)
			.leftJoin(commonAttributeValue.brtbProduct, product)
			.where(where);
		
		List<BrtbProductCommonAttrValue> products = query.list(commonAttributeValue);
		return products;
	}

	@Override
	public BrtbProductCommonAttrValue getCommonAttributeValueByProductIdCommonAttributeId(
			Long productId, Long commonAttributeId) {
		JPQLQuery query = new JPAQuery(em());
		
		QBrtbProduct product = QBrtbProduct.brtbProduct;
		QBrtbProductCommonAttrValue commonAttributeValue = QBrtbProductCommonAttrValue.brtbProductCommonAttrValue;
		QBrtbCommonAttribute commonAttribute = QBrtbCommonAttribute.brtbCommonAttribute;
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(product.recordStatus.eq(RecordStatus.O))
			 .and(commonAttribute.recordStatus.eq(RecordStatus.O))
			 .and(commonAttributeValue.brtbCommonAttribute.id.eq(commonAttributeId))
			 .and(commonAttributeValue.brtbProduct.id.eq(productId));
		
		query.from(commonAttributeValue, product, commonAttribute)
		.where(where);
		
		BrtbProductCommonAttrValue result = query.uniqueResult(commonAttributeValue);
		return result;
	}
}
